package com.yf.dxapp.module.app.service.behavior;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yf.dxapp.module.app.controller.admin.behavior.vo.BehaviorPageReqVO;
import com.yf.dxapp.module.app.controller.admin.behavior.vo.BehaviorSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.behavior.BehaviorDO;
import com.yf.dxapp.module.app.dal.mysql.behavior.BehaviorMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yf.dxapp.module.app.enums.ErrorCodeConstants.DATA_NOT_EXISTS;

/**
 * 行为记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BehaviorServiceImpl implements BehaviorService {

    @Resource
    private BehaviorMapper behaviorMapper;

    @Override
    public Long createBehavior(BehaviorSaveReqVO createReqVO) {
        // 插入
        BehaviorDO behavior = BeanUtils.toBean(createReqVO, BehaviorDO.class);
        behaviorMapper.insert(behavior);
        // 返回
        return behavior.getId();
    }

    @Override
    public void updateBehavior(BehaviorSaveReqVO updateReqVO) {
        // 校验存在
        validateBehaviorExists(updateReqVO.getId());
        // 更新
        BehaviorDO updateObj = BeanUtils.toBean(updateReqVO, BehaviorDO.class);
        behaviorMapper.updateById(updateObj);
    }

    @Override
    public void deleteBehavior(Long id) {
        // 校验存在
        validateBehaviorExists(id);
        // 删除
        behaviorMapper.deleteById(id);
    }

    private void validateBehaviorExists(Long id) {
        if (behaviorMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public BehaviorDO getBehavior(Long id) {
        return behaviorMapper.selectById(id);
    }

    @Override
    public PageResult<BehaviorDO> getBehaviorPage(BehaviorPageReqVO pageReqVO) {
        return behaviorMapper.selectPage(pageReqVO);
    }

    @Override
    public void record(Long loginUserId, Long refId, Integer type, String behavior, int minTime) {
        List<BehaviorDO> behaviors = behaviorMapper.selectList(new LambdaQueryWrapperX<BehaviorDO>()
                .eqIfPresent(BehaviorDO::getCreator, loginUserId.toString())
                .eqIfPresent(BehaviorDO::getRefId, refId)
                .eqIfPresent(BehaviorDO::getType, type)
                .ge(BehaviorDO::getCreateTime, LocalDateTime.now().minusSeconds(minTime))
                .orderByDesc(BehaviorDO::getCreateTime)
        );

        BehaviorDO lastBehavior = behaviors.stream().findFirst().orElse(null);

        if (lastBehavior == null) {
            BehaviorSaveReqVO createReqVO = new BehaviorSaveReqVO();
            createReqVO.setType(type);
            createReqVO.setRefId(refId);
            createReqVO.setRemark(behavior);
            createReqVO.setCreatorId(loginUserId);
            this.createBehavior(createReqVO);
        } else {
            // 持续进行的行为，更新最新行为即可
            BehaviorSaveReqVO updateReqVO = new BehaviorSaveReqVO();
            updateReqVO.setId(lastBehavior.getId());
            updateReqVO.setType(type);
            updateReqVO.setRefId(refId);
            updateReqVO.setRemark(behavior);
            updateReqVO.setCreatorId(loginUserId);
            this.updateBehavior(updateReqVO);
        }
    }

}